package org.aeon.gamechatnest.util;

import org.aeon.gamechatnest.dto.SecurityUserDto;
import org.aeon.gamechatnest.pojo.User;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author: MissingAeon
 * @date: 2024/5/10 17:07
 * @description: 用于快速获取当前登录用户的信息
 */
public class SecurityUtil {
    /**
     * 获取当前登录用户的信息
     *
     * @return
     */
    public static User getUser() {
        return ((SecurityUserDto) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUserCache().getUser();
    }

    /**
     * 获取当前登录包装用户
     *
     * @return
     */
    public static SecurityUserDto getSecurityUser() {
        return (SecurityUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /**
     * 获取当前登录用户ID
     *
     * @return
     */
    public static Long getUserId() {
        return getUser().getUserId();
    }

    /**
     * 获取当前用户名
     *
     * @return
     */
    public static String getUsername() {
        return getSecurityUser().getUsername();
    }

    /**
     * 获取当前用户进入的服务器
     */
    public static Long getServer() {
        return getSecurityUser().getUserCache().getServerId();
    }

    /**
     * 获取当前用户进入的频道
     */
    public static Long getChannel() {
        return getSecurityUser().getUserCache().getChannelId();
    }
}
